//
//  AIRatingView.m
//  AIRatingView
//
//  Created by Ailix on 15/4/2.
//  Copyright (c) 2015年 Ailix. All rights reserved.
//

#import "AIRatingView.h"
#import "AIMacro.h"

@interface AIRatingView ()

@property (nonatomic,copy) NSMutableArray *imageViews;

@end


@implementation AIRatingView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (void)awakeFromNib
{
    [self commonInit];
    
}




- (void)commonInit
{
    _imageViews = [NSMutableArray array];
    self.maxRating = 5.;
    _middleMargin = 5.;
    _editable = YES;
//    _sideMargin = 0.;
    
}


- (void)setCurrentRating:(CGFloat)currentRating
{
    if (_currentRating != currentRating) {
        _currentRating = currentRating;
        [self refreshImages];
        [_delegate ratingView:self didUpdateRating:currentRating];
    }
}


- (void)setMaxRating:(CGFloat)maxRating
{

    if (_maxRating != maxRating) {
        _maxRating = maxRating;
        [self.imageViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.imageViews removeAllObjects];
        
        for (int i = 0; i < maxRating; i++) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [self addSubview:imageView];
            [self.imageViews addObject:imageView];
        }
        [self refreshImages];
    }
    
}


- (void)setNotSelectedImage:(UIImage *)notSelectedImage
{
    if (_notSelectedImage != notSelectedImage) {
        _notSelectedImage = notSelectedImage;
        [self refreshImages];
    }
}

- (void)setHalfSelectedImage:(UIImage *)halfSelectedImage
{
    if (_halfSelectedImage != halfSelectedImage) {
        _halfSelectedImage = halfSelectedImage;
        [self refreshImages];
    }
    
}

- (void)setFullSelectedImage:(UIImage *)fullSelectedImage
{
    if (_fullSelectedImage != fullSelectedImage) {
        _fullSelectedImage = fullSelectedImage;
        [self refreshImages];
    }
}

- (void)refreshImages
{
    [self.imageViews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        if (_currentRating >= idx + 1) {
            imageView.image = _fullSelectedImage;
        }else if (_currentRating > idx){
            imageView.image = _halfSelectedImage;
        }else{
            imageView.image = _notSelectedImage;
        }
    }];
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    [self configWithTouchPoint:touchPoint];
    [super touchesBegan:touches withEvent:event];
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    [self configWithTouchPoint:touchPoint];
    [super touchesMoved:touches withEvent:event];
}


- (void)configWithTouchPoint:(CGPoint)touchPoint
{
    
    
    if (!_editable) {
        return;
    }
    
   __block CGFloat newRating = 0;
    
    [self.imageViews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        
        if (touchPoint.x > CGRectGetMidX(imageView.frame)){
            newRating = idx + 1;
            *stop = YES;

        }
        else if (touchPoint.x > CGRectGetMinX(imageView.frame)) {
            newRating = idx + .5;
            *stop = YES;
        }

    }];
    
    self.currentRating = newRating;

    
    /*
    CGFloat newRating = 0;

    for(int i = self.imageViews.count - 1; i >= 0; i--) {
        UIImageView *imageView = [self.imageViews objectAtIndex:i];
        if (touchPoint.x > CGRectGetMidX(imageView.frame)) {
            newRating = i+1;
            break;
        }else if (touchPoint.x > CGRectGetMinX(imageView.frame)){
            newRating = i + .5;
            break;
        }
    }
 
*/
    
}

- (void)layoutSubviews
{
    CGFloat imageWidth = (CGRectGetWidth(self.frame) - 2*_sideMargin - (self.imageViews.count -1)*_middleMargin) / self.imageViews.count;
    CGFloat imageHeight = CGRectGetHeight(self.frame);
    [self.imageViews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
        
        imageView.frame = CGRectMake(_sideMargin + idx*(imageWidth + _middleMargin), 0, imageWidth, imageHeight);
        
    }];

    [super layoutSubviews];
}

@end
